package com.example.elmspringboot.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.elmspringboot.controller.utils.Result;
import com.example.elmspringboot.domain.Admin;
import com.example.elmspringboot.domain.User;
import com.example.elmspringboot.service.Impl.UserServiceImpl;
import com.example.elmspringboot.service.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.example.elmspringboot.domain.User;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@RestController
//@RequestMapping("/api")
@RequestMapping
public class TestController {
    @Resource
    private UserService userService;
    @GetMapping("/test")
    public String test(){
        return "好阿里山扩大扩大酒啊就是大家安静";
    }
    @PostMapping("/update")
    public Result upload(@RequestParam("file") MultipartFile file, @RequestParam Integer userId) {
        if (file.isEmpty()) {
            return new Result(false,null,"图片不能为空");
        }
        String filePath = "D:/Java2022Soft/elm_vue_webpack/public/images/user/"+file.getOriginalFilename();
        System.out.println(filePath);
        File saveDir = new File(filePath);
        if (!saveDir.getParentFile().exists())
            saveDir.getParentFile().mkdirs();
        try {
            file.transferTo(saveDir);
            User user = userService.getById(userId);
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("user_id",user.getId());
            updateWrapper.set("user_img","/images/user/" + file.getOriginalFilename());
            userService.update(user,updateWrapper);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new Result(true," /images/user/" + file.getOriginalFilename(),"成功了");
    }
}
